#!/bin/bash

set -e
shopt -s extglob

if [ "${ACTION}" == "clean" ]; then
	echo Remove "${OBJECT_FILE_DIR}"
	[ -n "${OBJECT_FILE_DIR}" -a -d "${OBJECT_FILE_DIR}" ] && rm -rf "${OBJECT_FILE_DIR}"
else
	# For yasm and pkg-config
	PATH=$PATH:/opt/homebrew/bin:/usr/local/bin

	echo Build in ${OBJECT_FILE_DIR}
	BUILT=""
	for ARCH in $ARCHS; do
		mkdir -p "${OBJECT_FILE_DIR}/${ARCH}"
		cd "${OBJECT_FILE_DIR}/${ARCH}"
		if [ -f config.h ]; then
			echo Skipping configure
		else
			if [ "${CONFIGURATION}" == "Debug" ]; then
				FLAGS="--disable-optimizations"
			else
				FLAGS=
			fi
			if [ $ARCH == arm64 ]; then
				FLAGS="$FLAGS --enable-cross-compile --enable-vfp --enable-neon"
			elif [ $ARCH == x86_64 ]; then
				FLAGS="$FLAGS --enable-cross-compile --cpu=haswell"
			fi
			PKG_CONFIG_LIBDIR="${BUILT_PRODUCTS_DIR}/${ARCH}/lib/pkgconfig" "${SRCROOT}/${TARGET_NAME}/configure" --prefix="${BUILT_PRODUCTS_DIR}/${ARCH}" --arch=${ARCH} --cc="clang -arch ${ARCH}" ${FLAGS} --extra-ldexeflags=-liconv --disable-stripping --enable-gpl --disable-doc --disable-indevs --disable-outdevs --disable-network --disable-avdevice --disable-muxers --disable-encoders --disable-bsfs --disable-filters --disable-protocols --disable-autodetect --enable-appkit --enable-avfoundation --enable-bzlib --enable-coreimage --enable-iconv --enable-libaom --enable-zlib --enable-audiotoolbox --enable-videotoolbox --enable-filter=scale --enable-muxer=image2 --enable-encoder=png --enable-protocol=file --enable-protocol=pipe
		fi
		make -j`sysctl -n hw.physicalcpu` install
	done

	# Combine into Universal
	echo Installing into ${BUILT_PRODUCTS_DIR}/universal

	mkdir -p "${BUILT_PRODUCTS_DIR}/universal/bin"
	for EXE in ffmpeg ffprobe; do
		lipo -create "${BUILT_PRODUCTS_DIR}/"+(${ARCHS/ /|})"/bin/${EXE}" -output "${BUILT_PRODUCTS_DIR}/universal/bin/${EXE}"
        if [ -n "$DEVELOPMENT_TEAM" -a -n "$PRODUCT_BUNDLE_IDENTIFIER" ]; then
            ENTITLEMENTS=`mktemp -t buildffmpeg`
            if [ "${CONFIGURATION}" == "Debug" ]; then
                echo -e '<plist version="1.0">\n<dict>\n<key>com.apple.security.inherit</key>\n<true/>\n<key>com.apple.security.get-task-allow</key>\n<true/>\n</dict>\n</plist>' > "${ENTITLEMENTS}"
            else
                echo -e '<plist version="1.0">\n<dict>\n<key>com.apple.security.inherit</key>\n<true/>\n</dict>\n</plist>' > "${ENTITLEMENTS}"
            fi
            codesign -s "${DEVELOPMENT_TEAM}" -i "${PRODUCT_BUNDLE_IDENTIFIER}.${EXE}" -o runtime --entitlements "${ENTITLEMENTS}" "${BUILT_PRODUCTS_DIR}/universal/bin/${EXE}"
            rm "${ENTITLEMENTS}"
        fi
	done

	mkdir -p "${BUILT_PRODUCTS_DIR}/universal/include"
	# Assumes there aren't any material client impacting differences in config between archs
	cp -p "${OBJECT_FILE_DIR}/${ARCHS/ */}/config.h" "${BUILT_PRODUCTS_DIR}/universal/include/"

	mkdir -p "${BUILT_PRODUCTS_DIR}/universal/lib"
	for LIB in libavcodec libavfilter libavformat libavutil libpostproc libswresample libswscale; do
		cp -pr "${BUILT_PRODUCTS_DIR}/${ARCHS/ */}/include/${LIB}" "${BUILT_PRODUCTS_DIR}/universal/include/"
		lipo -create "${BUILT_PRODUCTS_DIR}/"+(${ARCHS/ /|})"/lib/${LIB}.a" -output "${BUILT_PRODUCTS_DIR}/universal/lib/${LIB}.a"
	done
fi
